package com.example.demo.controller;

import com.example.demo.anno.Log;
import com.example.demo.common.Result;
import com.example.demo.pojo.*;
import com.example.demo.service.StudentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import jakarta.websocket.Session;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j

public class StudentController {
    private String account;//前后端传的账号
    @Autowired
    private StudentService studentService;

    @Autowired
    private RedisTemplate redisTemplate;

//根据account查询学生信息
    @Log
    @GetMapping("/student/{account}")
    public Result getByAccount(@PathVariable String account){
        Student student = studentService.getByAccount(account);
        return Result.success(student);
    }
    //修改学生信息
    @Log
    @PutMapping("/student")
    public Result update(@RequestBody Student student){
        studentService.update(student);
        return Result.success();
    }

    //保存学生简历
    @Log
    @PostMapping("/student")
    public Result save(@RequestBody Student student){
        studentService.save(student);
        return Result.success();
    }



    //投递简历给企业
    @Log
    @PostMapping("/toudi")
    public Result touDi(@RequestBody Company_Student companyStudent, HttpServletRequest httpServletRequest){
        System.out.println(httpServletRequest.getSession().getAttribute("admin"));
        studentService.touDi(companyStudent);
        return Result.success();
    }

    //根据account查询学生已投递简历信息 在company_student表
    @Log
    @GetMapping("/stu/{accountStu}")
    public Result getByStuAccount(@PathVariable String accountStu){
        List<Company_Student> list = new ArrayList<>();
        list = studentService.getByStuAccount(accountStu);
//        Company_Student companyStudent = studentService.getByStuAccount(accountStu);
        return Result.success(list);
    }

    //redis所有学生信息
    @Log
    @GetMapping("/studentInfo")
    public Result selectUser(){
        List<Student> studentList;
//        //将生成的信息缓存到redis,并且设置有效期为60分钟
//        redisTemplate.opsForValue().set("1","2",60, TimeUnit.MINUTES);
//        System.out.println("缓存成功");
        //先从redis中获取缓存数据(可以一个key 对应一个数组
//         studentList = (List<Student>) redisTemplate.opsForValue().get("studentInfo");
//
//        if (studentList != null){
//            //如果存在,直接返回数据
//            log.info("所有学生信息缓存数据存在,返回给前端");
//            return Result.success(studentList);
//        }
        //如果不存在,则查询数据库,并加入缓存

        studentList = studentService.selectStudentInfo();
//        redisTemplate.opsForValue().set("studentInfo",studentList);
        log.info("所有学生数据不存在,查询数据库");
        //        //将生成的信息缓存到redis,并且设置有效期为60分钟
//        redisTemplate.opsForValue().set("1","2",60, TimeUnit.MINUTES);
//        System.out.println("缓存成功");
//        List<Student> List = studentService.selectStudentInfo();

        return Result.success(studentList);
    }

    @Log
    @GetMapping("/selectYaoQing/{accountStu}")
    public Result getAccountByAccountStu(@PathVariable String accountStu){
        System.out.println(accountStu);
        List<YaoQing> list = studentService.getAccountByAccountStu(accountStu);
        System.out.println(list);
        return Result.success(list);
    }

//    @GetMapping("/recruitInfo")
//    public List<RecruitInfo> selectUser(){
//        List<RecruitInfo> List = recruitInfoService.selectRecruitInfo();
//        return List;
//    }

}
